#_init__.py
import smtplib
from flask_mail import Mail, Message
from flask import Flask
from app.controller import  Member, user,admin,request,experiment,course,assignqV,HelpTopicElement,helptopic,llmAnswer

# 定义注册蓝图方法
def register_blueprints(app):
    app.register_blueprint(Member.MemberBP,url_prefix='/Member')
    app.register_blueprint(user.userBP,url_prefix='/user')
    app.register_blueprint(admin.adminBP,url_prefix='/admin')
    app.register_blueprint(request.requestBP,url_prefix='/request')
    app.register_blueprint(experiment.experimentBP,url_prefix='/experiment')
    app.register_blueprint(course.courseBP,url_prefix='/course')
    app.register_blueprint(assignqV.assignqVBP,url_prefix='/assignqV')
    app.register_blueprint(HelpTopicElement.HelpTopicElementBP,url_prefix='/HelpTopicElement')
    app.register_blueprint(helptopic.helptopicBP,url_prefix='/helptopic')
    app.register_blueprint(llmAnswer.llmAnswerBP,url_prefix='/llmAnswer')
# 注册插件(数据库关联)
def register_plugin(app):
    from app.models.base import db
    db.init_app(app)
    # create_all要放到app上下文环境中使用
    with app.app_context():
        db.create_all()


def create_app():
    app = Flask(__name__)
    # app.config.from_object('app.config.setting') # 基本配置(setting.py) 
    app.config.from_object('app.config.secure') # 重要参数配置(secure.py)
    app.secret_key = 'your_secret_key'
    app.config.update(
        MAIL_SERVER='smtp.163.com',
        MAIL_PORT=25,
        MAIL_USE_TLS=True,
        MAIL_USERNAME='zj2585306788@163.com',
        MAIL_PASSWORD='ZTLVGCSLEQILTDVS'
    )
    
    # Initialize Flask-Mail
    mail = Mail(app)
    # 注册蓝图与app对象相关联
    register_blueprints(app)
    # 注册插件(数据库)与app对象相关联
    register_plugin(app)
    # 一定要记得返回app
    return app,mail
